package org.voidness.oje2d.gui;

import org.lwjgl.input.Keyboard;
import org.voidness.oje2d.Engine;
import org.voidness.oje2d.GLColor;
import org.voidness.oje2d.GLFont;
import org.voidness.oje2d.GLImage;

/** A text box widget to use with the GUI engine. */
public class TextBox extends Widget {
    /** The normal image index */
    public static int NORMAL = 0;

    /** The focused image index */
    public static int FOCUS = 1;

    /** The font to use to draw the text box text */
    private GLFont font = null;

    /** The text of the text box */
    private String text = null;

    /** The maximum number of characters in the text box */
    private int maxSize;

    /** The images for the 2 images (normal and with focus) */
    private GLImage[] images = null;

    /** Whether this is a password box or not; Do we mask the text? */
    private boolean password;

    /** The text used to mask */
    private String illusionText = null;

    /**
     * Constructor for the textbox class.
     * 
     * @param mWidth The textbox's width
     * @param mHeight The textbox's height
     * @param mFont The font to use for the text
     * @param mMaxSize The maximum text size
     */
    public TextBox(int mWidth, int mHeight, GLFont mFont, int mMaxSize) {
        // Call the widget's constructor
        super(mWidth, mHeight);

        // Get the textbox in 0,0 relative to the parent widget
        xPos = 0;
        yPos = 0;

        font = mFont;
        text = new String();
        illusionText = new String();
        maxSize = mMaxSize;
        password = false;
    }

    /**
     * A more complex constructor for the textbox.
     * 
     * @param mX The textbox's position in the X axis
     * @param mY The textbox's position in the Y axis
     * @param mWidth The textbox's width
     * @param mHeight The textbox's height
     * @param mFont The font to use for the text
     * @param mMaxSize The maximum text size
     */
    public TextBox(int mX, int mY, int mWidth, int mHeight, GLFont mFont,
            int mMaxSize) {
        super(mX, mY, mWidth, mHeight);

        font = mFont;
        text = new String();
        illusionText = new String();
        maxSize = mMaxSize;
        password = false;
    }

    /**
     * Defines the images for the textbox.
     * 
     * @param mImages A GLImage array with the textbox's images
     */
    public void setImages(GLImage[] mImages) {
        images = mImages;
        images[NORMAL].setWidth(width);
        images[NORMAL].setHeight(height);
        images[FOCUS].setWidth(width);
        images[FOCUS].setHeight(height);
    }

    /**
     * In here we overload the default key pressed event method so we can
     * capture what is beeing written to the text box and perform the required
     * actions.
     * 
     * @param mKey The key beeing pressed
     * @param mChar The correspondent character
     */
    public void keyPressed(int mKey, char mChar) {
        // Call the parent method
        super.keyPressed(mKey, mChar);

        if (focus) {
            // Special keys first
            if (mKey == Keyboard.KEY_BACK && text.length() > 1) {
                text = text.substring(0, text.length() - 1);
                illusionText =
                        illusionText.substring(0, illusionText.length() - 1);
            } else if (mKey == Keyboard.KEY_BACK && text.length() == 1) {
                text = "";
                illusionText = "";
            }
            // Keys we want to ignore
            else if (/* (new String("" + mChar)).equals(" ") || */mKey == Keyboard.KEY_TAB)
                return;
            else if (text.length() < maxSize) {
                // Check for shift keys
                if (Engine.getInstance().isShiftKeyDown())
                    text += mChar;
                else
                    text += (new String("" + mChar)).toLowerCase();

                illusionText += "*";
            }
        }
    }

    /**
     * Get the text in the text box.
     * 
     * @return The current text of the box
     */
    public String getText() {
        return text;
    }

    /**
     * Clears the text
     */
    public void clear() {
        text = new String("");
    }

    /**
     * (Re)defines whether this text box is a password or not
     * 
     * @param mPassword If true, this is a password box, i.e., we mask the text
     */
    public void setPassword(boolean mPassword) {
        password = mPassword;
    }

    /**
     * Draws the widget and all sub-widgets.
     */
    public void draw(GLColor mColor) {
        // Draw only if visible
        if (!visible)
            return;

        for (int i = 0; i < images.length; i++)
            images[i].setColor(mColor);

        // Check which state is the textbox in
        if (focus)
            images[FOCUS]
                    .draw(xPos + parent.getXPos(), yPos + parent.getYPos());
        else
            images[NORMAL].draw(xPos + parent.getXPos(), yPos
                    + parent.getYPos());

        // Draw the text
        if (!password)
            font.print(text, xPos + parent.getXPos(), yPos + parent.getYPos()
                    + 2, 0.6f, GLColor.BLACK);
        else
            font.print(illusionText, xPos + parent.getXPos(), yPos
                    + parent.getYPos() + 2, 0.6f, GLColor.BLACK);

        // Cycle the widget list and draw all
    }
}